/**
 * 
 */
package com.kunbo.cn.controller.core.inventory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.kunbo.cn.entity.core.inventory.CarrierUser;
import com.kunbo.cn.service.core.inventory.CarrierUserService;
import com.kunbo.cn.util.date.SDate;
import com.kunbo.cn.util.retu.CommonResult;
/**
 *承运商用户管理
 * @author bjl  
 * @date 2019年3月14日 
 */
@RestController
@RequestMapping("/CarrierUser")
public class CarrierUserController {
	@Autowired
	private JdbcTemplate jdbc;
	@Autowired
	private CarrierUserService Causervice;
	/**
	 * 添加库存单
	 * 
	 */
	@PostMapping("/add")
	public CommonResult add(CarrierUser cuser,HttpServletRequest request) {
		Causervice.save(cuser);
		return new CommonResult(true,"操作成功",null);
	}
	
	@GetMapping("/createCode")
	public Map<String,Object> createCode(){
		Map<String,Object> map=new HashMap<String,Object>();
		 String organize="山西复兴煤业";
		 String prex = "CY";
		 String noticeCode= prex+SDate.getSystemDateYMDHMSToString();
		 map.put("noticeCode", noticeCode);
		 String result = "";
		 result=SDate.getRandom();//6位随机数字
			Long t=3l;
			if(result!=null) {
				while(t>0) {
					String sql="";
					sql +="select\r\n" ; 
					sql +="count(*) as num   \r\n";
					sql +="FROM business_carrier_user_manage  d\r\n" ; 
					sql +="where login_account='"+result+"'\r\n";
					List<Map<String,Object>> list = jdbc.queryForList(sql);
					 t = (Long) list.get(0).get("num");
					 if(t>0) {
						 result ="";
						 result=SDate.getRandom();
					 }
				}
			}
			 map.put("result",result);
		return map;
	}
	@PutMapping("/update")
	public CommonResult update(CarrierUser cuser) {
		Causervice.update(cuser);
		return new CommonResult(true,"操作成功",null);
	}
	
	@DeleteMapping("/delete")
	public CommonResult delete(String id) {
		Causervice.delete(id);
		return new CommonResult(true,"操作成功",null);
	}

	/**
	 * 详情
	 * 
	 */
	@GetMapping("/findById")
	public CarrierUser findById(String id) {
		CarrierUser d = Causervice.findById(id);
		return d;
	}
	
	/**
	 *条件分页查询
	 * 
	 */
	@GetMapping("/findCarrierUser")
	public Page<CarrierUser> findCarrierUser(String carrier_code,String carrier,String login_account,Integer pageNo,Integer pageSize) {
		Page<CarrierUser> page = Causervice.findCarrierUser(carrier_code, carrier,login_account,pageNo, pageSize);
		return page;
	}
}
